package com.changwen.currentactivity;

import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;

import com.lzf.easyfloat.EasyFloat;
import com.lzf.easyfloat.enums.ShowPattern;
import com.lzf.easyfloat.interfaces.OnInvokeView;

/**
 * <pre>
 *     author: lcw
 *     date  : 2022/7/15
 *     desc  : ∧_∧
 * </pre>
 */
public class FloatUtils {

    public static String floatTag = "CurrentActivityFloat";

    public static void showFloat(Context context, String tag, String appName, String currentActivity) {
        if (TextUtils.isEmpty(appName)) {
            appName = "Activity记录";
            currentActivity = "";
        }
        View floatView = EasyFloat.getFloatView(tag);
        if (floatView == null) {
            String final_appName = appName;
            String final_currentActivity = currentActivity;
            EasyFloat.with(context)
                    .setTag(tag)
                    .setLayout(R.layout.float_current_activity, new OnInvokeView() {
                        @Override
                        public void invoke(View view) {
                            setFloatContent(view, final_appName, final_currentActivity);
                        }
                    })
                    .setShowPattern(ShowPattern.ALL_TIME)
                    .setLayoutChangedGravity(Gravity.START)
                    .setMatchParent(false, false)
                    .show();
        } else {
            updateFloat(tag, appName, currentActivity);
        }
    }

    public static void updateFloat(String tag, String appName, String currentActivity) {
        View floatView = EasyFloat.getFloatView(tag);
        setFloatContent(floatView, appName, currentActivity);
    }

    private static void setFloatContent(View floatView, String appName, String currentActivity) {
        if (floatView != null) {
            TextView tv_app_name = floatView.findViewById(R.id.tv_app_name);
            TextView tv_current_activity = floatView.findViewById(R.id.tv_current_activity);
            tv_app_name.setText(appName);
            tv_current_activity.setText(currentActivity);
        }
    }
}
